package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import cn.wolfcode.luowowo.website.util.UMEditorUploader;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * 前端游记
 */
@Controller
@RequestMapping("travel")
public class TravelController {

    @Reference
    private  ITravelService travelService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelCommentService travelCommentService;
    @RequestMapping("")
    public String index(Model model, @ModelAttribute("qo") TravelQuery qo){
        PageInfo page = travelService.query(qo);
        model.addAttribute("pageInfo",page);
        return "travel/list";
    }

    @RequestMapping("input")
    public String input(Model model,Long id){
        List<Destination> list = destinationService.getDestsByDeep(3);
        model.addAttribute("dests",list);

        if (id != null){
            Travel tv = travelService.get(id);
            TravelContent content = travelService.getContent(id);
            tv.setTravelContent(content);
            model.addAttribute("tv",tv);
        }
        return "travel/input";

    }

    //上传图片
    @Value("${file.path}")
    private String filePath;
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, filePath);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel, HttpServletRequest request, HttpServletResponse response){
        //从token中获取当前用户信息
        String token = CookieUtil.getToken(request, response);
        UserInfo author = userInfoRedisService.getUserByToken(token);

        travel.setAuthor(author);//作者
        Long id = travelService.saveOrUpdate(travel);
        return AjaxResult.SUCCESS.addData(id);

    }
    @RequestMapping("/detail")
    public String detail(Model model,Long id){
        //detail
        Travel detail = travelService.get(id);
        TravelContent content = travelService.getContent(id);
        detail.setTravelContent(content);
        model.addAttribute("detail",detail);
        
        //toasts
        Destination dest = detail.getDest();
        List<Destination> toasts = destinationService.getToasts(dest.getId());
        toasts.remove(toasts.size()-1);
        model.addAttribute("toasts",toasts);

        //sds
        List<StrategyCatalog> sds = strategyDetailService.queryStrategyTop3(dest.getId());
        model.addAttribute("sds",sds);

        //ts
        List<Travel> ts = travelService.getViewNumTop3(dest.getId());
        model.addAttribute("ts",ts);

        //评论list
        TravelCommentQuery qo = new TravelCommentQuery();
        qo.setTravelId(id);
        qo.setCurrentPage(1);
        qo.setPageSize(Integer.MAX_VALUE);
        List list = travelCommentService.query(qo).getContent();
        model.addAttribute("list",list);

        return "travel/detail";
    }
    //用户对象注入
    @RequestMapping("/info")
    @ResponseBody
    public Object info(@UserParam UserInfo userInfo){
        return userInfo;
    }

    @RequestMapping("/userUpdata")
    @ResponseBody
    public Object userUpdata(UserInfo userInfo){//springmvc自带解析器
        return userInfo;
    }

    @RequestMapping("/commentAdd")
    public String commentAdd(TravelComment comment,Model model,int floor,@UserParam UserInfo userInfo){
        //添加评论数据
        comment.setUserId(userInfo.getId());
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUsername(userInfo.getNickname());

       TravelComment c = travelCommentService.save(comment);
       model.addAttribute("c",c);
       model.addAttribute("floor",floor+1);




        return "travel/commentTpl";
    }

}
